package com.guanzi.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guanzi.server.entity.Food;
import com.guanzi.server.entity.dto.FoodSaveDTO;
import com.guanzi.server.mapper.FoodMapper;
import com.guanzi.server.query.FoodOrderListQuery;
import com.guanzi.server.service.IFoodService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author guanzi
 * @since 2021-06-04
 */
@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements IFoodService {

    @Override
    public List<Food> getFoodList( FoodOrderListQuery foodOrderListQuery) {
        if ( foodOrderListQuery.getId() == null || foodOrderListQuery.getId() == "") {
            return null;
        }
        int id = Integer.parseInt(foodOrderListQuery.getId());
        if ( foodOrderListQuery.getName() != null && foodOrderListQuery.getName() != "" ) {
            return baseMapper.selectList(new QueryWrapper<Food>().like("name", foodOrderListQuery.getName()).eq("e_store_id", foodOrderListQuery.getId()).eq("is_del",0));
        }
        return baseMapper.selectList(new QueryWrapper<Food>().eq("e_store_id", foodOrderListQuery.getId()).eq("is_del",0));
    }

    /**
     * 菜品删除
     * @param foodId
     */
    @Override
    public void foodDelete(Integer foodId) {
        // 逻辑删除
        Food food = baseMapper.selectById(foodId);
        food.setIsDel(1);
        baseMapper.updateById(food);
    }

    /**
     * 清零计数器
     * 批量置0
     */
    @Override
    public void clearNumber() {
        List<Food> foods = baseMapper.selectList(new QueryWrapper<Food>().eq("is_del", 0));
        for (Food f : foods) {
            f.setNumber(0);
            baseMapper.updateById(f);
        }
    }

    /**
     * 更新计数器
     *  0代表+
     *  1代表-
     * @param idAndOperation
     */
    @Override
    public int updateNumber(String idAndOperation) {
        // 将id和操作符分开
        String id = idAndOperation.substring( 0, idAndOperation.length()-1 );
        String addOrReduce = idAndOperation.substring( idAndOperation.length()-1 );
        // 对菜品计数器做更改
        Food food = baseMapper.selectOne(new QueryWrapper<Food>().eq("is_del", 0).eq("id",Integer.parseInt(id) ));
        if (Integer.parseInt(addOrReduce) == 0){
            food.setNumber( food.getNumber()+1 );
            return baseMapper.updateById(food);
        }
        if (Integer.parseInt(addOrReduce) == 1){
            food.setNumber( food.getNumber()-1 >= 0 ? food.getNumber()-1 : 0 );
            return baseMapper.updateById(food);
        }
        return 0;
    }

    /**
     * 获取已点列表
     *      查找到number属性不为空，就放进list里面
     * @return
     */
    @Override
    public List<Food> getFoodListData() {
        return baseMapper.selectList(new QueryWrapper<Food>().eq("is_del", 0).gt("number", 0));
    }

    /**
     *  菜品保存方法（含图片）
     * @param foodSaveDTO
     * @return
     */
    @Override
    public Boolean saveFood(FoodSaveDTO foodSaveDTO) {
        //判断是否有id
            //有就是更新操作
            //无就是增加操作
        if ( foodSaveDTO.getId() != null ) {
            //更新
            Food food = baseMapper.selectById( foodSaveDTO.getId() );
            // 查询名称是否被使用
            Food temp = baseMapper.selectOne(new QueryWrapper<Food>().eq("name", food.getName()));
            if ( temp != null && !(temp.getId().equals(food.getId())) ) {
                return false;
            }
            food.setName( foodSaveDTO.getName() );
            food.setImg( foodSaveDTO.getImg() );
            food.setRemark( foodSaveDTO.getRemark() );
            food.setEStoreId( foodSaveDTO.getEStoreId() );
            food.setPrice( foodSaveDTO.getPrice() );
            baseMapper.updateById(food);
            return true;
        } else {
            //增加
            Food food = new Food();
            food.setName( foodSaveDTO.getName() );
            food.setImg( foodSaveDTO.getImg() );
            food.setRemark( foodSaveDTO.getRemark() );
            food.setEStoreId( foodSaveDTO.getEStoreId() );
            food.setPrice( foodSaveDTO.getPrice() );

            baseMapper.insert(food);
            return true;
        }

    }
}
